from apps.myapp.models import StockModel, StockModelInst
from apps.vadmin.op_drf.serializers import CustomModelSerializer
from rest_framework import serializers


class StockModelSerializer(CustomModelSerializer):
    """
    项目管理 简单序列化器
    """

    class Meta:
        model = StockModel
        fields = '__all__'


class StockModelCreateUpdateSerializer(CustomModelSerializer):
    """
    创建/更新时的列化器
    """

    # 此处可写定制的 创建/更新 内容
    def validate(self, attrs: dict):
        return super().validate(attrs)

    class Meta:
        model = StockModel
        fields = '__all__'
class ExportStockModelSerializer(CustomModelSerializer):
    """
    导出 项目管理 简单序列化器
    """
    person__username = serializers.SerializerMethodField(read_only=False)
    dept__deptName = serializers.SerializerMethodField(read_only=False)

    def get_person__username(self, obj):
        return "" if not hasattr(obj, 'person') else obj.person.username

    def get_dept__deptName(self, obj):
        return "" if not hasattr(obj, 'dept') else obj.dept.deptName

    class Meta:
        model = StockModel
        fields = ('id', 'name', 'code', 'creator', 'modifier',
                  'description')




class StockModelInstSerializer(CustomModelSerializer):
    """
    项目管理 简单序列化器
    """

    class Meta:
        model = StockModelInst
        fields = '__all__'

class StockModelInstCreateUpdateSerializer(CustomModelSerializer):
    """
    创建/更新时的列化器
    """

    # 此处可写定制的 创建/更新 内容
    def validate(self, attrs: dict):
        return super().validate(attrs)

    class Meta:
        model = StockModelInst
        fields = '__all__'

class ExportStockModelInstSerializer(CustomModelSerializer):
    """
    导出 项目管理 简单序列化器
    """
    person__username = serializers.SerializerMethodField(read_only=False)
    dept__deptName = serializers.SerializerMethodField(read_only=False)

    def get_person__username(self, obj):
        return "" if not hasattr(obj, 'person') else obj.person.username

    def get_dept__deptName(self, obj):
        return "" if not hasattr(obj, 'dept') else obj.dept.deptName

    class Meta:
        model = StockModelInst
        fields = (  'name' , 'description')
